package com.yuyou.fn.educational.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yuyou.fn.educational.service.IContinueClassSettingService;
import com.yuyou.fn.educational.service.IPeriodService;
import com.yuyou.fn.educational.vo.ContinueClassSettingVo;
import com.yuyou.fn.platform.service.IAreaTeamService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class ContinueClassSettingServiceImpl implements IContinueClassSettingService {

    private static final String PRE_KEY = "continue:class:setting:%d";

    @Resource
    private RedisTemplate<String,String> redisTemplate ;

    @Resource
    private IPeriodService periodService ;

    @Resource
    private IAreaTeamService areaTeamService ;


    @Cacheable("CacheOfFindContinueClassSettingByPeriodId")
    @Override
    public ContinueClassSettingVo findContinueClassSettingByPeriodId(Long periodId) {
        ContinueClassSettingVo continueClassSettingVo = new ContinueClassSettingVo();
        continueClassSettingVo.setPeriodId(periodId);
        String itemJSON = redisTemplate.opsForValue().get(String.format(PRE_KEY, periodId));
        if(itemJSON == null ){
            continueClassSettingVo.setItemList(Collections.EMPTY_LIST);
        }else{
            continueClassSettingVo.setItemList(JSONObject.parseObject(itemJSON,new TypeReference<List<ContinueClassSettingVo.Item>>(){}));
        }
        return continueClassSettingVo;
    }

    @Cacheable("CacheOfFindBaseContinueClassSettingByPeriodIds")
    @Override
    public List<ContinueClassSettingVo> findBaseContinueClassSettingByPeriodIds(List<Long> periodIds) {
        List<ContinueClassSettingVo> continueClassSettingVoList = new ArrayList<>(periodIds.size());
        for(Long  periodId : periodIds ){
            ContinueClassSettingVo continueClassSettingVo = new ContinueClassSettingVo();
            continueClassSettingVo.setPeriodId(periodId);
            String itemJSON = redisTemplate.opsForValue().get(String.format(PRE_KEY, periodId));
            if(itemJSON == null ){
                continueClassSettingVo.setItemList(Collections.EMPTY_LIST);
            }else{
                continueClassSettingVo.setItemList(JSONObject.parseObject(itemJSON,new TypeReference<List<ContinueClassSettingVo.Item>>(){}));
            }
            continueClassSettingVoList.add(continueClassSettingVo);
        }
        return continueClassSettingVoList;
    }

    @CacheEvict(value = {"CacheOfFindBaseContinueClassSettingByPeriodIds","CacheOfFindContinueClassSettingByPeriodId" } , allEntries = true )
    @Override
    public void saveOrUpdateContinueClassSetting(ContinueClassSettingVo continueClassSettingVo) {
        if(continueClassSettingVo.getPeriodId() == null){
            throw new IllegalArgumentException("Illegal arg:periodId");
        }

        if(continueClassSettingVo.getItemList() == null || continueClassSettingVo.getItemList().isEmpty()){
            throw new IllegalArgumentException("Illegal arg:itemList");
        }
        redisTemplate.opsForValue().set(String.format(PRE_KEY,continueClassSettingVo.getPeriodId()) , JSONObject.toJSONString(continueClassSettingVo.getItemList()));
    }
}
